import pandas
import numpy
from collections import Counter
from matplotlib import pyplot
data = pandas.read_csv('外贸数据.csv', encoding = 'gb18030')
data2 = pandas.read_csv('数据导出2.csv', encoding = 'gb18030')
data3 = pandas.read_csv('news.csv', encoding = 'gb18030')
# print(data)
def ciyun(data):
    trade_modes = list(data.商品名称)
    trade_modes = Counter(trade_modes)
def zhexiantu(data):
    trade_partners = list(data.贸易伙伴名称)
    trade_partners = dict(Counter(trade_partners))
    trade_partners = sorted(trade_partners.items(), key = lambda x: x[1], reverse=True)
    print(trade_partners[0:10])
    trade_partners = dict(trade_partners)
    print(trade_partners)
    # print(trade_partners)
    keys = [i for i in trade_partners.keys()][ : 10]
    values = [i for i in trade_partners.values()][ : 10]
    # print(keys)
    # print(values)
    pyplot.figure(figsize=(9, 7))
    pyplot.rcParams['font.sans-serif'] = 'SimHei'
    pyplot.plot(keys, values, 'ro--')
    pyplot.xlabel('国家')
    pyplot.ylabel('次数')
    pyplot.xticks(range(10), keys, rotation = 45)
    pyplot.title("与各国外贸次数折线图")
    pyplot.savefig('与各国外贸次数折线图.png')
    pyplot.show()

def bingtu(data):
    trade_ways = list(data.贸易方式名称)
    trade_ways = dict(Counter(trade_ways))
    keys = [i for i in trade_ways.keys()]
    values = [int(i) for i in trade_ways.values()]
    pyplot.rcParams['font.family'] = 'SimHei'
    pyplot.rcParams['axes.unicode_minus'] = False
    x = values
    pyplot.pie(x)
    colors = pyplot.get_cmap('Blues')(numpy.linspace(0.2, 0.7, len(x)))
    print(colors)
    pyplot.title('贸易方式饼图')
    pyplot.pie(x, autopct='%1.1f%%',colors=colors ,pctdistance=0.6,labels=keys,labeldistance=1.2)
    pyplot.savefig("贸易方式饼图.png")
    pyplot.show()
def zhuxingtu(data):
    registration_place = list(data.注册地名称)
    price = list(data.人民币)
    price = [int(i.replace(',', '')) for i in price]
    list1 = []
    for i in range(len(price)):
        list1.append([registration_place[i],price[i]])
    # print(list1)
    dict1 = dict(list1)
    print(len(list1))
    print(dict1)
    print(len(dict1))
def news(data2):
    title = list(data2.新闻标题)
    url = list(data2.链接)
    date = list(data2.月)
    for i in range(len(title)):
        text = f'''<a href="{url[i]}"  target="_blank"><li>
             <div class="title">{title[i]}</div>
             <div class="time">2023年0{date[i]}</div></li></a>'''
        print(text)

if __name__ == ("__main__"):
    zhuxingtu(data2)